/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jfreefall;

/**
 *
 * @author joshi
 */



import java.awt.Font;

import java.text.DecimalFormat;
import java.util.LinkedList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RefineryUtilities;

/**
 * A demonstration application showing how to create a vertical combined chart.
 *
 */
public class grafico extends JFrame {

    DecimalFormat df = new DecimalFormat("0.000");
    public static LinkedList<nodo> lista;
    /**
     * Constructs a new demonstration application.
     *
     * @param title  the frame title.
     */
    public grafico(final String title) {

        super(title);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setIconImage(new ImageIcon(getClass().getResource("/img/labs.png")).getImage());
        final JFreeChart chart = createCombinedChart();
        final ChartPanel panel = new ChartPanel(chart, true, true, true, false, true);
        panel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(panel);

    }

    /**
     * Creates a combined chart.
     *
     * @return the combined chart.
     */
    private JFreeChart createCombinedChart() {

        // create subplot 1...
        final XYDataset data1 = createDataset1();
        final XYItemRenderer renderer1 = new StandardXYItemRenderer();
        final NumberAxis rangeAxis1 = new NumberAxis("Altura");
        final XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
        subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

        double altura = java.lang.Double.valueOf(pool.taltura.getText());
        System.out.println(altura+" altura");
        final XYTextAnnotation annotation = new XYTextAnnotation("R^2: "+df.format(pool.det) , 20.0  , altura/2);
        annotation.setFont(new Font("SansSerif", Font.PLAIN, 10));
       // annotation.setRotationAngle(Math.PI / 4.0);
        subplot1.addAnnotation(annotation);



        // parent plot...
        final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Distancia"));
        plot.setGap(10.0);

        // add the subplots...
        plot.add(subplot1, 1);
      //  plot.add(subplot2, 1);
        plot.setOrientation(PlotOrientation.VERTICAL);

        // return a new chart containing the overlaid plot...
        return new JFreeChart("Altura vrs Distancia",
                              JFreeChart.DEFAULT_TITLE_FONT, plot, true);

    }

    /**
     * Creates a sample dataset.
     *
     * @return Series 1.
     */
    private XYDataset createDataset1() {

        // create dataset 1...
        final XYSeries series1 = new XYSeries("y^="+df.format(pool.b1)+"x+"+df.format(pool.b0));

        final XYSeries series2 = new XYSeries("Caida libre");
        lista = pool.ex.getlista();

        double x=0;
        for (int i=0; i<lista.size(); i++){
            series2.add(lista.get(i).x,lista.get(i).y);
            if (i==1 || i==lista.size()-1){
                x=lista.get(i).x;
                series1.add(x, x*pool.b1+pool.b0 );
            }
            
        }
      





        final XYSeriesCollection collection = new XYSeriesCollection();
        collection.addSeries(series1);
        collection.addSeries(series2);


        return collection;

    }

    /**
     * Creates a sample dataset.
     *
     * @return Series 2.
     */
    


    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {

        final grafico demo = new grafico("Gráfico de Regresión Lineal");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}